package com.nanhai.buddhist.college.module.collegebase.service.studentchangelog;

import com.nanhai.buddhist.college.framework.common.util.collection.CollectionUtils;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.studentchangelog.vo.StudentChangeLogCreateReqVO;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.studentchangelog.vo.StudentChangeLogExportReqVO;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.studentchangelog.vo.StudentChangeLogPageReqVO;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.studentchangelog.vo.StudentChangeLogUpdateReqVO;
import com.nanhai.buddhist.college.module.collegebase.convert.studentchangelog.StudentChangeLogConvert;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.studentchangelog.StudentChangeLogDO;
import com.nanhai.buddhist.college.module.collegebase.dal.mysql.studentchangelog.StudentChangeLogMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.studentchangelog.vo.*;
import com.nanhai.buddhist.college.framework.common.pojo.PageResult;

import static com.nanhai.buddhist.college.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.nanhai.buddhist.college.module.collegebase.enums.ErrorCodeConstants.*;

/**
 * 信息异动记录 Service 实现类
 *
 * @author AKai
 */
@Service
@Validated
public class StudentChangeLogServiceImpl implements StudentChangeLogService {

    @Resource
    private StudentChangeLogMapper studentChangeLogMapper;

    @Override
    public String createStudentChangeLog(StudentChangeLogCreateReqVO createReqVO) {
        // 插入
        StudentChangeLogDO studentChangeLog = StudentChangeLogConvert.INSTANCE.convert(createReqVO);
        studentChangeLogMapper.insert(studentChangeLog);
        // 返回
        return studentChangeLog.getId();
    }

    @Override
    public void updateStudentChangeLog(StudentChangeLogUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateStudentChangeLogExists(updateReqVO.getId());
        // 更新
        StudentChangeLogDO updateObj = StudentChangeLogConvert.INSTANCE.convert(updateReqVO);
        studentChangeLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteStudentChangeLog(String id) {
        // 校验存在
        this.validateStudentChangeLogExists(id);
        // 删除
        studentChangeLogMapper.deleteById(id);
    }

    private void validateStudentChangeLogExists(String id) {
        if (studentChangeLogMapper.selectById(id) == null) {
            throw exception(STUDENT_CHANGE_LOG_NOT_EXISTS);
        }
    }

    @Override
    public StudentChangeLogDO getStudentChangeLog(String id) {
        return studentChangeLogMapper.selectById(id);
    }

    @Override
    public List<StudentChangeLogDO> getStudentChangeLogList(Collection<String> ids) {
        return CollectionUtils.isEmpty(ids) ? Collections.emptyList() : studentChangeLogMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<StudentChangeLogDO> getStudentChangeLogPage(StudentChangeLogPageReqVO pageReqVO) {
        return studentChangeLogMapper.selectPage(pageReqVO);
    }

    @Override
    public List<StudentChangeLogDO> getStudentChangeLogList(StudentChangeLogExportReqVO exportReqVO) {
        return studentChangeLogMapper.selectList(exportReqVO);
    }

}
